package script

import (
	"context"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gfile"
	"github.com/gogf/gf/v2/text/gstr"
)

// 搜索所有的脚本 <in> 要检索的目录
func (s *sScript) scanScript(ctx context.Context, in string) (out g.MapStrStr, err error) {

	s.log().Debug(ctx, "搜索路径:", in)
	file, err := gfile.ScanDirFile(in, `*.sh`)
	if err != nil {
		return
	}

	fileBat, err := gfile.ScanDirFile(in, `*.bat`)
	if err != nil {
		return
	}

	file = append(file, fileBat...)
	if g.IsEmpty(file) {
		return
	}

	out = make(g.MapStrStr)
	for _, f := range file {
		s.log().Debug(ctx, "读取到的文件", f)
		out[gfile.Name(f)] = gstr.Replace(gfile.Join(in, gfile.Basename(f)), "\\", "/")
	}
	return
}
